<?php
/**
 * Created by PhpStorm.
 */

use Biz\model\salesRebate\SalesCoupon as mSalesCoupon;
use Biz\module\Classes\Coupon as clsCoupon;
use Biz\service\activity\Coupon as sCoupon;
/**
 * @deprecated
 */
class PullCouponAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;
    const ACTION_TYPE = "coupon";

    public function main()
    {
        $couponCode = $this->getRequest()->getPost('coupon_code', '');
        $payAmount = $this->getRequest()->getPost('pay_amount', 0);
        $skuToken = $this->getRequest()->getPost('sku_token', '');

        $from = clsCoupon::PULL_COUPON_INAPP;
        if ($this->appType == self::APPTYPE_PARAGRAPH_MINIPROGRAM) {
            $from = clsCoupon::PULL_COUPON_INPARAGRAPH;
        }

        $result = sCoupon::pullCoupon($this->uid, $couponCode, $skuToken, $from, $payAmount);

        $this->salesCouponInfo($result);
        if (isset($result['msg'])) {
            $data['msg'] = $result['msg'];
            unset($result['msg']);
        }
        $data['coupon_info'] = $result;
        $this->setSuccess($data);
    }

    private function salesCouponInfo(&$result)
    {
        $result['is_designer'] = 0;
        if ($result && $result['coupon_id'] && $result['code']) {
            $where = [
                'AND' => [
                    'status' => mSalesCoupon::STATUS_OPEN,
                    'OR' => [
                        'person_coupon_code' => $result['code'],
                        'company_coupon_code' => $result['code']
                    ]
                ]
            ];
            $salesCouponInfo = mSalesCoupon::getOneByWhere('*', $where);
            if ($salesCouponInfo) {
                $result['is_designer'] = 1;
                $result['title'] = '设计师' . $result['title'];
            }
        }
    }
}